digit		[0-9]
letter		[A-Za-z]
alphanum	({letter}|{digit})

%{
	#include <stdio.h>
	#include "y.tab.h"
	#include "datatypes.h"
	#include "internal_kb.h"
	#define YYSTYPE Expression
	extern YYSTYPE yylval;
	#define OTHER_TOKEN -1
	int last_token = OTHER_TOKEN;
	extern int alternation_count;
	extern int alternation_stack[];
	enum concept_description_type last_expression;
	enum concept_description_type current_expression;
	// last_expression = -1;
	// current_expression = -1;
%}

%%

\n			{ ++yylineno; }

			/* skip whitespace */
[ \t]			;

			/* skip comments */
"//".*\n		;

			/* full IRI, not very precise */
"<http://"({alphanum}|\.|\/|\?|-|~)+"#"({alphanum}|-|_|\.)+">" {
			 if (last_token == OBJECTSOMEVALUESFROM) {
			 	yylval.role = create_atomic_role(yytext);
				last_token = OTHER_TOKEN;
			 	return FULLIRI; 
			 }
			 // in all other cases, the IRI is in a concept description
		 	 yylval.concept = create_atomic_concept(yytext);
			 // last_expression = current_expression;
			 // current_expression = ATOMIC_CONCEPT;
			 return FULLIRI; 
			}

			/* base IRI, not very precise */
"<http://"({alphanum}|\.|\/|\?|\*|-|_|~|#)+">"	{
				return BASEIRI; 
			}

			/* parantheses, etc ... */
[()<>=:]		{ return *yytext; }

			/* ontology */
Prefix			{ return PREFIX; }
Ontology 		{ return ONTOLOGY; }
Import			{ return IMPORT; }

			/* skip declarations, irrelevant for reasoning */
Declaration.*		;	// { return DECLARATION; }

AnnotationAssertion.*	;		/* skip annotation assertions */

Annotation.*		;		/* skip ontology annotations */

TransitiveObjectProperty.*	;		/* skip transitive roles for now */

Class			{ return CLASS;}

EquivalentClasses	{ return EQUIVALENTCLASSES; }

SubClassOf		{ return SUBCLASSOF; }

ObjectIntersectionOf	{ 
			 // ++nesting_depth;
			 printf("(l)object_intersection\n");

			 // printf("(l1)alternation_count:%d\n",alternation_count);
			 // printf("(l1)alternation stack top:%d\n",alternation_stack[alternation_count]);
			 // printf("(l1)last expression:%d\n",last_expression);
			 // fflush(stdout);
			 // last_expression = current_expression;
			 // current_expression = CONJUNCTION;


			 if (last_expression == EXISTENTIAL_RESTRICTION)
				++alternation_count;
			 ++alternation_stack[alternation_count];
			 last_expression = CONJUNCTION;
			 // printf("(l2)alternation_count:%d\n",alternation_count);
			 // printf("(l2)alternation stack top:%d\n",alternation_stack[alternation_count]);
			 // printf("(l2)last expression:%d\n",last_expression);
			 // fflush(stdout);
			 return OBJECTINTERSECTIONOF; 
			}

ObjectSomeValuesFrom	{ 
			 printf("(l)object_somevalues_from\n");
			 // printf("(l1)last expression:%d\n",last_expression);
			 last_expression = EXISTENTIAL_RESTRICTION;
			 // last_expression = current_expression;
			 // current_expression = EXISTENTIAL_RESTRICTION;
			 last_token = OBJECTSOMEVALUESFROM;
			 // printf("(l2)last expression:%d\n",last_expression);
			 return OBJECTSOMEVALUESFROM;
			}

ObjectProperty		{ return OBJECTPROPERTY; }

{alphanum}+		{ return IDENTIFIER; }


			/* anything else is an error */
.			{ ECHO ; yyerror("unexpected character"); }

%%

int yywrap(void) {
	return 1;
}
